﻿using HardcoreGame.Common.Players;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;

namespace HardcoreGame.Content.Items.Consumables.Healing
{
    public class FirstAidKit : ModItem
    {
        public int healPoints = 100;
        public override void SetDefaults()
        {
            Item.width = 32;
            Item.height = 32;
            Item.maxStack = Item.CommonMaxStack;
            Item.consumable = true;
            Item.useTime = 120;
            Item.useAnimation = 120;
            Item.useStyle = ItemUseStyleID.HoldUp;
            Item.UseSound = SoundID.Item4;
            Item.value = Item.buyPrice(0, 1, 0);
            Item.rare = ItemRarityID.Blue;
            Item.noMelee = true;
        }
        public override void ModifyTooltips(List<TooltipLine> tooltips)
        {
            tooltips.Add(new TooltipLine(Mod, "HealPoints", Language.GetTextValue("Mods.HardcoreGame.ToolTip.7") + $"{healPoints}/100"));
        }     
        public override bool? UseItem(Player player)
        {
            if (player.whoAmI == Main.myPlayer)
            {
                var modPlayer = player.GetModPlayer<FracturePlayer>();
                var injuredParts = new List<FractureLocation>();
                foreach (FractureLocation location in Enum.GetValues(typeof(FractureLocation)))
                {
                    if (modPlayer.GetPartHealth(location) < 1f)
                    {
                        injuredParts.Add(location);
                    }
                }
                if (injuredParts.Count == 0)
                {
                    return false;
                }

                FractureLocation selectedPart = injuredParts[Main.rand.Next(injuredParts.Count)];
                float currentHealth = modPlayer.GetPartHealth(selectedPart);
                float healAmount = MathHelper.Clamp(0.15f, 0f, 1f - currentHealth);
                modPlayer._partHealth[selectedPart] = currentHealth + healAmount;
                healPoints -= 15;
                if (healPoints <= 0)
                {
                    healPoints = 0;
                    Item.stack--;
                }

                for (int i = 0; i < 15; i++)
                {
                    Dust.NewDust(player.position, player.width, player.height,
                        DustID.HealingPlus, 0, 0, 100, default, 1.3f);
                }
                SoundEngine.PlaySound(SoundID.Item4 with { Pitch = 0.3f });
                return true;
            }

            return false;
        }

        public override void AddRecipes()
        {
            CreateRecipe()
                .AddIngredient(ItemID.HealingPotion, 2)
                .AddIngredient(ItemID.Silk, 10)
                .AddIngredient(ItemID.Gel, 20)
                .AddTile(TileID.Anvils)
                .Register();
        }
        public override void SaveData(TagCompound tag)
        {
            tag["healPoints"] = healPoints;
        }
        public override void LoadData(TagCompound tag)
        {
            healPoints = tag.GetInt("healPoints");
        }
        public override void NetSend(BinaryWriter writer)
        {
            writer.Write(healPoints);
        }

        public override void NetReceive(BinaryReader reader)
        {
            healPoints = reader.ReadInt32();
        }
    }
}